package com.airline.service;

import com.airline.dao.OrderDAO;
import com.airline.dao.RefundDAO;
import com.airline.entity.Order;

public class RefundService {
    private OrderDAO orderDAO = new OrderDAO();
    private RefundDAO refundDAO = new RefundDAO();
    private FlightStockService stockService = new FlightStockService(); // 余票服务

    /**
     * 退票业务：验证订单存在 + 回退余票 + 记录退票
     */
    public boolean refundTicket(int orderId) {
        // 1. 校验订单是否存在
        Order order = orderDAO.findOrderById(orderId);
        if (order == null) {
            System.out.println("订单号 " + orderId + " 不存在，退票失败！");
            return false;
        }

        // 2. 回退余票（退票成功后余票+1）
        if (!stockService.increaseStock(order.getFlightNo())) {
            System.out.println("余票回退失败，退票失败！");
            return false;
        }

        // 3. 记录退票信息（插入refund表）
        refundDAO.addRefund(orderId);
        System.out.println("订单号 " + orderId + " 退票成功！余票已回退。");
        return true;
    }
}